package com.loser.gateway.netty.coder;

import com.loser.gateway.base.CmdMeta;
import com.loser.gateway.utils.CmdUtils;
import com.loser.module.base.proto.base.InnerRequest;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

@ChannelHandler.Sharable
public class ProtostuffDecoder extends MessageToMessageDecoder<ByteBuf> {

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {

        CmdUtils.Info info = CmdUtils.split(byteBuf);
        int cmd = info.getCmd();
        byte[] bytes = info.getData();
        InnerRequest innerRequest = new InnerRequest();
        CmdMeta cmdMeta = CmdUtils.getCmdMeta(cmd);
        innerRequest.setMethodCmd(cmd);
        innerRequest.setService(cmdMeta.getServiceName());
        innerRequest.setHandlerClass(cmdMeta.getHandlerClass());
        innerRequest.setMethod(cmdMeta.getMethod());
        innerRequest.setArgClass(cmdMeta.getArgClass());
        innerRequest.setBytes(bytes);
        list.add(innerRequest);

    }

}
